﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Xts
{
    namespace XInterBank
    {
        namespace TradingClient
        {
            public class XTradingQueryAgent_async
            {
                protected Ice.Communicator x_ic;
                protected Ice.Logger x_logger;
                protected IXTradingQueryPrx x_tradingQueryPrx;
                protected IXAmiTradingQueryCBHandler x_cbHandler;

                public bool IsValid { get { return x_tradingQueryPrx != null; } }

                public XTradingQueryAgent_async(ref Ice.Communicator ic, ref Ice.Logger logger,
                    ref IXAmiTradingQueryCBHandler cbHandler)
                {
                    x_ic = ic;
                    x_logger = logger;
                    x_tradingQueryPrx = null;
                    x_cbHandler = cbHandler;
                }

                public bool Initialize(string tradingQueryDsb)
                {
                    try
                    {
                        Ice.ObjectPrx objPrx = x_ic.stringToProxy(tradingQueryDsb);
                        x_tradingQueryPrx = IXTradingQueryPrxHelper.checkedCast(objPrx);
                        return true;
                    }
                    catch(Ice.Exception ex)
                    {
                        x_logger.error(ex.Message);
                    }
                    return false;
                }

                public bool QueryOrderInfo(string orderId, string customOrderId)
                {
                    if (!IsValid)
                    {
                        x_logger.error("The XTradingQueryAgent_async object is not valid.");
                        return false;
                    }

                    try
                    {
                        XTradingQuery_QueryOrderInfo_Cookies cookies = new XTradingQuery_QueryOrderInfo_Cookies(orderId, customOrderId);
                        XAmiTradingQuery_QueryOrderInfoCallback cb =
                            new XAmiTradingQuery_QueryOrderInfoCallback(cookies, ref x_cbHandler, ref x_logger);
                        x_tradingQueryPrx.begin_QueryOrderInfo(orderId, customOrderId).whenCompleted(cb.QueryOrderInfoCB, cb.FailureQueryOrderInfoCB);
                        return true;
                    }
                    catch (Ice.Exception ex)
                    {
                        x_logger.error(ex.Message);
                    }

                    return false;
                }

                public bool BatchQueryOrderInfo(string accountId)
                {
                    if (!IsValid)
                    {
                        x_logger.error("The XTradingQueryAgent_async object is not valid.");
                        return false;
                    }

                    try
                    {
                        XTradingQuery_BatchQueryOrderInfo_Cookies cookies = new XTradingQuery_BatchQueryOrderInfo_Cookies(accountId);
                        XAmiTradingQuery_BatchQueryOrderInfoCallback cb =
                            new XAmiTradingQuery_BatchQueryOrderInfoCallback(cookies, ref x_cbHandler, ref x_logger);
                        x_tradingQueryPrx.begin_BatchQueryOrderInfo(accountId).whenCompleted(cb.BatchQueryOrderInfoCB, cb.FailureBatchQueryOrderInfoCB);
                        return true;
                    }
                    catch (Ice.Exception ex)
                    {
                        x_logger.error(ex.Message);
                    }

                    return false;
                }

                public bool QueryOrderFill(string orderId, string customOrderId)
                {
                    if (!IsValid)
                    {
                        x_logger.error("The XTradingQueryAgent_async object is not valid.");
                        return false;
                    }

                    try
                    {
                        XTradingQuery_QueryOrderFill_Cookies cookies = new XTradingQuery_QueryOrderFill_Cookies(orderId, customOrderId);
                        XAmiTradingQuery_QueryOrderFillCallback cb =
                            new XAmiTradingQuery_QueryOrderFillCallback(cookies, ref x_cbHandler, ref x_logger);
                        x_tradingQueryPrx.begin_QueryOrderFill(orderId, customOrderId).whenCompleted(cb.QueryOrderFillCB, cb.FailureQueryOrderFillCB);
                        return true;
                    }
                    catch (Ice.Exception ex)
                    {
                        x_logger.error(ex.Message);
                    }

                    return false;
                }

                public bool BatchQueryOrderFill(string accountId)
                {
                    if (!IsValid)
                    {
                        x_logger.error("The XTradingQueryAgent_async object is not valid.");
                        return false;
                    }

                    try
                    {
                        XTradingQuery_BatchQueryOrderFill_Cookies cookies = new XTradingQuery_BatchQueryOrderFill_Cookies(accountId);
                        XAmiTradingQuery_BatchQueryOrderFillCallback cb =
                            new XAmiTradingQuery_BatchQueryOrderFillCallback(cookies, ref x_cbHandler, ref x_logger);
                        x_tradingQueryPrx.begin_BatchQueryOrderFill(accountId).whenCompleted(cb.BatchQueryOrderFillCB, cb.FailureBatchQueryOrderFillCB);
                        return true;
                    }
                    catch (Ice.Exception ex)
                    {
                        x_logger.error(ex.Message);
                    }

                    return false;
                }
            }
        }
    }
}
